package com.bdqn.t350.aop;

import com.bdqn.t350.service.UserService;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;

import java.util.Arrays;

public class UserServiceLogger {

    Logger logger = Logger.getLogger(UserServiceLogger.class);

    //前置增强
    public void before(JoinPoint joinPoint) {
        logger.info("前置增强。。。。。。。。。。。。。。");
        logger.info("调用" + joinPoint.getTarget() + "的" + joinPoint.getSignature().getName()
                + "方法.方法入参:" + Arrays.toString(joinPoint.getArgs()));

    }

    //后置增强
    public void afterReturning(JoinPoint joinPoint, Object result) {
        logger.info("后置增强。。。。。。。。。。。。。。");
        logger.info("调用" + joinPoint.getTarget() + "的" + joinPoint.getSignature().getName()
                + "方法.方法返回值" + result);
    }

    //异常增强
    public void afterThrowing(JoinPoint joinPoint, RuntimeException e) {
        logger.info("异常增强。。。。。。。。。。。。。。");
        logger.error(joinPoint.getSignature().getName() + "方法发生了异常" + e);
    }

    //最终增强-finally
    public void afterLogger(JoinPoint joinPoint) {
        logger.info("最终增强..........");
    }

    //环绕增强
    public Object aroundLogger(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("调用 " + joinPoint.getTarget() + "的"
                + joinPoint.getSignature().getName() + "方法,方法入参:"
                + Arrays.toString(joinPoint.getArgs()));
        try {
            Object result = joinPoint.proceed();
            logger.info("调用" + joinPoint.proceed() + "的" + joinPoint.getSignature().getName() + "方法,方法返回值:" + result);
            return result;
        } catch (Exception e) {
            logger.error(joinPoint.getSignature().getName() + "方法发生异常:" + e);
            throw e;
        } finally {
            logger.info(joinPoint.getSignature().getName() + "方法执行结束");
        }
    }
}
